﻿#include "WRWorkDevicesGuiPart.h"
#include "WRWorkDevicesGuiView.h"
#include "../WRSoft-Utility/Runtime/WRClass.h"
#include "../WRSoft-GuiApp-Working-Common/Runtime/WRWorkMainGuiPartActivator.h"

#include <QLabel>
#include <QTabWidget>
#include <QHBoxLayout>

//表示工作设备管理部件创建器。
class WRWorkDevicesGuiPartActivator : public WRWorkMainGuiPartActivator
{
public:
    //初始化工作设备管理部件创建器。
    inline WRWorkDevicesGuiPartActivator(void)
    {
        QChar className[] = WRWorkDevicesGuiPartStrArr;
        WRClass::enroll(WRWorkDevicesGuiPartClass, QString(className, WRWorkDevicesGuiPartStrLen), this);
    }
    //释放当前对象所占用的相关资源。
    ~WRWorkDevicesGuiPartActivator(void)
    {
        //add code here
    }

    //创建工作设备管理部件。
    WRWorkMainGuiPart* create(const QDomElement&)
    {
        return new WRWorkDevicesGuiPart();
    }
};
//唯一实例。
static WRWorkDevicesGuiPartActivator g_workDevicesGuiPartActivator;

//初始化界面组件。
WRWorkDevicesGuiPart::WRWorkDevicesGuiPart(void)
{
    m_widget = 0;
    m_current = -1;
}
//释放当前对象所占用的相关资源。
WRWorkDevicesGuiPart::~WRWorkDevicesGuiPart(void)
{
    //add code here
}

//初始部件。
void WRWorkDevicesGuiPart::init(const WRObjRef<WRWorkDaemon>& daemon)
{
    const WRArray<WRWorkDriver*>& drivers = daemon->drivers();
    if (drivers.count())
    {
        QTabWidget* tabWidget = new QTabWidget();
        for(WRu i = 0, l = drivers.count(); i < l; ++i)
        {
            WRWorkDriver* driver = drivers[i];
            tabWidget->addTab(new WRWorkDevicesGuiView(driver), driver->title());
        }
        m_current = 0;
        tabWidget->setCurrentIndex(0);
        QObject::connect(
                    tabWidget, SIGNAL(currentChanged(int)),
                    this, SLOT(onCurrentChanged(int)));
        m_widget = tabWidget;
    }
    else
    {
        QLabel* label = new QLabel(QStringLiteral("没有设备！"));
        label->setAlignment(Qt::AlignCenter);
        m_widget = label;
    }
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setContentsMargins(2, 2, 2, 2);
    layout->addWidget(m_widget);
    setLayout(layout);
}

//打开部件。
void WRWorkDevicesGuiPart::open(void)
{
    if (m_current != -1)
    {
        QTabWidget* widget = qobject_cast<QTabWidget*>(m_widget);
        //打开当前标签页
        WRWorkDevicesGuiView* guiView = qobject_cast<WRWorkDevicesGuiView*>(widget->widget(m_current));
        guiView->open();
    }
}
//关闭部件。
void WRWorkDevicesGuiPart::close(void)
{
    if (m_current != -1)
    {
        QTabWidget* widget = qobject_cast<QTabWidget*>(m_widget);
        //关闭当前标签页
        WRWorkDevicesGuiView* guiView = qobject_cast<WRWorkDevicesGuiView*>(widget->widget(m_current));
        guiView->close();
    }
}

//当标签页改变后引发此事件。
void WRWorkDevicesGuiPart::onCurrentChanged(int index)
{
    QTabWidget* widget = qobject_cast<QTabWidget*>(m_widget);
    //关闭上次标签页
    WRWorkDevicesGuiView* guiView = qobject_cast<WRWorkDevicesGuiView*>(widget->widget(m_current));
    guiView->close();
    //保存当前标签面
    m_current = index;
    //打开当前标签页
    guiView = qobject_cast<WRWorkDevicesGuiView*>(widget->widget(m_current));
    guiView->open();
}

//获取推荐尺寸。
QSize WRWorkDevicesGuiPart::sizeHint() const
{
    return QSize(800, 600);
}
